-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WrapIterImpl: add support for inputs with line breaks #123
Conversation
Hi Alex, thanks for the PR! I'm about to go to the US for 10 days so please don't be afraid if I don't get around to comment on their before February :-) My first thought was that this is something the caller could handle by splitting the input string on newlines and then mapping |
By the way, the build failure seems to be caused by a breaking change in a dependency - not because of your changes. I'll have to sort that out... |
Hi Martin! Sure, no worries! Yes, you're right that we could split the input first and then call Also, currently, calling Have a nice trip! |
Hi Alex, I'm back again :-) I think you're right: the current behavior is confusing... I had actually expected I've considered adding a Do you think we should put that kind of logic into Perhaps it's better to keep |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like how complex this function is already, so I tried to see if we could avoid adding more code than absolutely necessary here.
Please give it a try and also correct the other minor things. Thanks!
src/lib.rs
Outdated
|
||
#[test] | ||
fn multiline() { | ||
assert_eq!(fill("1 3 5 7\n1 3 5 7",11),"1 3 5 7\n1 3 5 7"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you run rustfmt
on the file? I believe it would insert a space after the ,
. Thanks!
src/lib.rs
Outdated
|
||
self.start = self.split + self.split_len; | ||
self.line_width += wrapper.subsequent_indent.width(); | ||
self.line_width -= self.line_width_at_split; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is redundant -- in total, self.line_width
becomes wrapper.subsequent_indent.width()
since you set it to self.line_width_at_split
above. So you can remove this line and simplify the previous line to a direct assignment:
self.line_width = wrapper.subsequent_indent.width();
src/lib.rs
Outdated
fn multiline() { | ||
assert_eq!(fill("1 3 5 7\n1 3 5 7",11),"1 3 5 7\n1 3 5 7"); | ||
assert_eq!(fill("1 3 5 7\n1 3 5 7",5),"1 3 5\n7\n1 3 5\n7"); | ||
assert_eq!(fill("1 3 5 7\n1 3 5 7",6),"1 3 5\n7\n1 3 5\n7"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little hard for me to figure out which corner-case each test is testing :-) Could you perhaps add multiple smaller tests if there is a common theme?
Here, I think a third line with
assert_eq!(fill("1 3 5 7\n1 3 5 7", 7), "1 3 5 7\n1 3 5 7");
could be interesting -- it would demonstrate that the code doesn't accidentally add an extra \n
.
src/lib.rs
Outdated
assert_eq!(fill("test\nabcdefghi\n",11),"test\nabcdefghi\n"); | ||
assert_eq!(fill("test\nabcdefghi\n\n",11),"test\nabcdefghi\n\n"); | ||
assert_eq!(fill("test\nabcdefghijklmnopq\n\n",11),"test\nabcdefghijk\nlmnopq\n\n"); | ||
assert_eq!(fill("test\nabcdefghijklmnopq abcdefghijk\n\n",11),"test\nabcdefghijk\nlmnopq\nabcdefghijk\n\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The length of "abcdefghijklmnopq"
is 17, so I'm not actually sure if there is a relation between that number and the width of 11?
|
||
// If this is not the final line, return the current line. Otherwise, | ||
// we will return the line with its line break after exiting the loop | ||
if self.split + self.split_len < self.source.len() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't actually try, but would it be possible to combine the logic here with the existing logic that kicks in when the current line it too long? Maybe by changing the conditions slightly:
if ch != '\n' && is_whitespace(ch) {
// ...
} else if ch == `\n` || self.line_width + char_width > wrapper.width {
// ...
}
No idea if it works! :-D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm maybe but I can't really seem to get it working this way ^^ feel free to commit if you find a way
Thanks for the feedback! I'm a bit busy at the moment but I'll get back to you in a bit |
Hi @axelchalon, no stress at all! Let me know if you think my suggestions are annoying or unhelpful, in which case I'll be happy to do them myself. Otherwise just take your time to iterate on this. I should have fixed the brokenness of |
Hey Martin! It's all good, no worries :)
Hmm, Rust already supports escaping line breaks and subsequent leading whitespace with
It certainly wouldn't feel intuitive for me to have to separate each line with an extra blank line. I would expect the function to simply do what it's supposed to do: wrap the lines that are too long. (Of course, we could say that this function could handle lists as well, but then again it would overreach its simple purpose / intended function.) |
Very good point! More elaborate wrapping logic should at the very least be put into a different function then. |
Thanks a lot, I've merged the branch and will make a release in one of the next days. |
Closes #122
does the trick but it needs reviewing!